package service

import (
	"ksd-social-api/commons/base/service"
	context3 "ksd-social-api/modules/msg/model/context"
	"ksd-social-api/modules/resume/model"
)

type ResumeTemplateCodeService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *ResumeTemplateCodeService) DuihuanResumeTemplate(code string, resumeTemplateId uint64, userId uint64, systemId uint64, systemName string) bool {
	resumeCode := resumeTemplateCodeMapper.CountResumeTemplateCode(code, resumeTemplateId, systemId)
	// 如果还没兑换，就开始兑换，生成小册订单，同时删除订单
	if resumeCode > 0 {
		// 删除兑换的订单
		resumeTemplateCodeMapper.UpdateResumeTemplateCode(code, resumeTemplateId, systemId)
		// 开始保存订单
		service.saveUserResumeTemplateOrder(code, resumeTemplateId, userId, systemId, systemName)
		// 兑换成功
		return true
	}
	return false
}

/**
 * 保存用户小册订单
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *ResumeTemplateCodeService) saveUserResumeTemplateOrder(code string, resumeTemplateId uint64, userId uint64, systemId uint64, systemName string) {
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := resumeTemplateCodeMapper.CountUserBuyResumeTemplateNo(userId, resumeTemplateId, systemId)
	if isBuyFlag == 0 {
		// 根据小册id查询小册信息
		resumeTemplate, _ := resumeTemplateMapper.GetById(resumeTemplateId, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)

		userBuyResumeTemplate := model.UserBuyResumeTemplate{}
		OutTradeNo := service.GetSnowWorkerIdString(8)
		// 处理返回结果
		userBuyResumeTemplate.Opid = resumeTemplateId
		userBuyResumeTemplate.Title = resumeTemplate.Title
		userBuyResumeTemplate.Cover = resumeTemplate.Cover
		userBuyResumeTemplate.UserId = userId
		userBuyResumeTemplate.Username = userVo.Username
		userBuyResumeTemplate.Avatar = userVo.Avatar
		userBuyResumeTemplate.Phone = userVo.Telephone
		userBuyResumeTemplate.Address = userVo.Address
		userBuyResumeTemplate.Nickname = userVo.Nickname

		userBuyResumeTemplate.Code = code
		userBuyResumeTemplate.PayMethod = 3
		userBuyResumeTemplate.PayMethodName = "兑换码"
		userBuyResumeTemplate.Tradeno = OutTradeNo
		userBuyResumeTemplate.Orderno = OutTradeNo
		userBuyResumeTemplate.OrderJson = ""
		userBuyResumeTemplate.SystemId = systemId
		userBuyResumeTemplate.SystemName = systemName
		userBuyResumeTemplate.Price = resumeTemplate.Price
		// 保存订单的接口
		resumeTemplateCodeMapper.SaveUserBuyResumeTemplate(userBuyResumeTemplate)
		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = resumeTemplate.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = systemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		messageMeContext.Url = "https://www.study512.com/template"
		messageMeService.SaveMessageMeBuyResumeTemplate(&messageMeContext)
	}
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (service *ResumeTemplateCodeService) DuihuanCoinResumeTemplate(resumeTemplateId uint64, userId uint64, systemId uint64, systemName string) bool {
	// 兑换成功
	return true
}
